package com.ww.scm.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ww.core.exception.ServiceException;
import com.ww.scm.constants.Constants;
import com.ww.scm.entity.mat.Bill;
import com.ww.scm.entity.mat.Transaction;
import com.ww.scm.entity.mat.TransactionDetail;
import com.ww.scm.entity.pub.Dictionary;
import com.ww.scm.repository.pub.DictionaryRepository;

public class StorageFactory {
	
	/*
	 * 日志类
	 */
	private static Logger logger = LoggerFactory.getLogger(StorageFactory.class);

	private final static StorageFactory instance = new StorageFactory();

	private StorageFactory() {

	}

	public static StorageFactory getInstance() {
		return instance;
	}
	
	public Bill createBill(DictionaryRepository dictionaryRepository,String billTypeId) {
		Dictionary dictionary=dictionaryRepository.get(billTypeId);
		return new Bill(dictionary);
	}
	
	public Transaction createTransaction(String transactionType) {
		if (Constants.TRANSACTION_TYPE_ADD_ID.equals(transactionType)) {
			return new Transaction(Constants.TRANSACTION_TYPE_ADD_ID);
		} else if (Constants.TRANSACTION_TYPE_SUBMIT_ID.equals(transactionType)) {
			return new Transaction(Constants.TRANSACTION_TYPE_SUBMIT_ID);
		}else if (Constants.TRANSACTION_TYPE_AUDIT_ID.equals(transactionType)) {
			return new Transaction(Constants.TRANSACTION_TYPE_AUDIT_ID);
		}
		logger.error("不能创建事务,无事务类型:"+transactionType+".");
		throw new ServiceException("不能创建事务,无事务类型:"+transactionType+".");
	}

	public TransactionDetail createTransactionDetail(String transactionDetailType) {
		if (Constants.TRANSACTION_DETAIL_TYPE_IN_ID.equals(transactionDetailType)) {
			return new TransactionDetail(Constants.TRANSACTION_DETAIL_TYPE_IN_ID);
		} else if (Constants.TRANSACTION_DETAIL_TYPE_OUT_ID.equals(transactionDetailType)) {
			return new TransactionDetail(Constants.TRANSACTION_DETAIL_TYPE_OUT_ID);
		}else if (Constants.TRANSACTION_DETAIL_TYPE_NONE_ID.equals(transactionDetailType)) {
			return new TransactionDetail(Constants.TRANSACTION_DETAIL_TYPE_NONE_ID);
		}
		logger.error("不能创建事务明细,无事务明细类型:"+transactionDetailType+".");
		throw new ServiceException("不能创建事务明细,无事务明细类型:"+transactionDetailType+".");
	}

}
